<?php

namespace app\model;

use app\controller\wmapi\IndexController;
use support\Db;
use support\Model;

class WaSupplierDomesticLogisticsFees
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_supplier_domestic_logistics_fees';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;


    public static function list($page,$pageNum,$fields,$condition)
    {

        $offset = ($page - 1) * $pageNum;
        $model = Db::table('wa_supplier_domestic_logistics_fees')
            ->select($fields)
            ->where($condition);
        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
        if ($list){
            foreach ($list as $k) {
                if ($k->account_information){
                    $k->account_information = json_decode($k->account_information);// 账户信息
                }
                $k->door_imgs = json_decode($k->door_imgs);
                // 1 增值税普通发票 2 增值税专用发票
//                if($k->billing_type == 1){
//                    $k->billing_type_name = '增值税普通发票';
//                }elseif($k->billing_type == 2){
//                    $k->billing_type_name = '增值税专用发票';
//                }else{
//                    $k->billing_type_name = '';
//                }

                if ($k->admin_id) {
                    $admins = WaAdmins::find(['id' => $k->admin_id]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $k->admin_name = $admins->nickname;
                    } else {
                        $k->admin_name = '';
                    }

                } else {
                    $k->admin_name = '';
                }
            }

        }

        return json_encode(['list' => $list, 'count' => $count]);
    }



    public static function all($fields,$condition)
    {

        $list = Db::table('wa_supplier_domestic_logistics_fees')
            ->select($fields)
            ->where($condition)
            ->orderBy('id', 'asc')
            ->get();
        if ($list) {
            foreach ($list as $k) {
                if ($k->account_information) {
                    $k->account_information = json_decode($k->account_information);// 账户信息
                }
            }
        }
        return json_encode($list);
    }




    public static function find($condition)
    {
        $res = Db::table('wa_supplier_domestic_logistics_fees')
            ->where($condition)
            ->first();
        if ($res){
            if ($res->account_information){
                $res->account_information = json_decode($res->account_information);// 账户信息
            }
            $res->door_imgs = json_decode($res->door_imgs);
            // 1 增值税普通发票 2 增值税专用发票
//            if($res->billing_type == 1){
//                $res->billing_type_name = '增值税普通发票';
//            }elseif($res->billing_type == 2){
//                $res->billing_type_name = '增值税专用发票';
//            }else{
//                $res->billing_type_name = '';
//            }

        }
        return json_encode($res);
    }


    public static function findOr($where,$whereOr)
    {
        $res = Db::table('wa_supplier_domestic_logistics_fees')
            ->where($where)
            ->orWhere($whereOr)
            ->first();
        if ($res){
            if ($res->account_information){
                $res->account_information = json_decode($res->account_information);// 账户信息
            }
            $res->door_imgs = json_decode($res->door_imgs);
            // 1 增值税普通发票 2 增值税专用发票
//            if($res->billing_type == 1){
//                $res->billing_type_name = '增值税普通发票';
//            }elseif($res->billing_type == 2){
//                $res->billing_type_name = '增值税专用发票';
//            }else{
//                $res->billing_type_name = '';
//            }

        }
        return json_encode($res);
    }

    public static function created($data){

        return Db::table('wa_supplier_domestic_logistics_fees')->insertGetId($data);

    }

    public static function updated($condition,$data){

        $res = Db::table('wa_supplier_domestic_logistics_fees')->where($condition)->update($data);
        return json_encode($res);

    }

    public static function deleted($condition){

        $res = Db::table('wa_supplier_domestic_logistics_fees')->where($condition)->delete();
        return json_encode($res);

    }



}